require "import"

local _M = {
  -- Message,
  STATUS_CONTINUE = 100,
  STATUS_SWITCHING_PROTOCOLS = 101,
  STATUS_PROCESSING = 102,
  STATUS_EARLY_HINTS = 103,

  -- Success ,
  STATUS_OK = 200,
  STATUS_CREATED = 201,
  STATUS_ACCEPTED = 202,
  STATUS_NON_AUTHORITATIVE_INFORMATION = 203,
  STATUS_NO_CONTENT = 204,
  STATUS_RESET_CONTENT = 205,
  STATUS_PARTIAL_CONTENT = 206,
  STATUS_MULTI_STATUS = 207,
  STATUS_ALREADY_REPORTED = 208,
  STATUS_IM_USED = 226,

  -- Redirect,
  STATUS_MULTIPLE_CHOICES = 300,
  STATUS_MOVED_PERMANENTLY = 301,
  STATUS_MOVED_TEMPORARILT = 302,
  STATUS_SEE_OTHER = 303,
  STATUS_NOT_MODIFIED = 304,
  STATUS_USE_PROXY = 305, --@Deprecated
  STATUS_SWITCH_PROXY = 306, --@Deprecated
  STATUS_TEMPORARY_REDIRECT = 307,
  STATUS_PERMANENT_REDIRECT = 308,

  -- Failure,
  STATUS_BAD_REQUEST = 400,
  STATUS_UNAUTHORIZED = 401,
  STATUS_PAYMENT_REQUIRED = 402,
  STATUS_FORBIDDEN = 403,
  STATUS_NOT_FOUND = 404,
  STATUS_METHOD_NOT_ALLOWED = 405,
  STATUS_NOT_ACCEPTABLE = 406,
  STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,
  STATUS_REQUEST_TIMEOUT = 408,
  STATUS_CONFLICT = 409,
  STATUS_GONE = 410,
  STATUS_LENGTH_REQUIRED = 411,
  STATUS_PRECONDITION_FAILED = 412,
  STATUS_REQUEST_ENTITY_TOO_LARGE = 413,
  STATUS_REQUEST_URL_TOO_LONG = 414,
  STATUS_UNSUPPORTED_MEDIA_TYPE = 415,
  STATUS_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
  STATUS_EXPECTATION_FAILED = 417,
  STATUS_IM_A_TEAPOT = 418, --@Deprecated
  STATUS_MISDIRECTED_REQUEST = 421,
  STATUS_UNPROCESSABLE_ENTITY = 422,
  STATUS_LOCKED = 423,
  STATUS_FAILED_DEPENDENCY = 424,
  STATUS_TOO_EARLY = 425,
  STATUS_UPGRADE_REQUIRED = 426,
  STATUS_UPGRADE_REQUIRED = 427,
  STATUS_PRECONDITION_REQUIRED = 428,
  STATUS_TOO_MANY_REQUESTS = 429,
  STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
  STATUS_RETRY_WITH = 449,
  STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451,

  -- Server Error,
  STATUS_INTERNAL_SERVER_ERROR = 500,
  STATUS_NOT_IMPLEMENTED = 501,
  STATUS_BAD_GATEWAY = 502,
  STATUS_SERVICE_UNAVAILABLE = 503,
  STATUS_GATEWAY_TIMEOUT = 504,
  STATUS_HTTP_VERSION_NOT_SUPPORTED = 505,
  STATUS_VARIANT_ALSO_NEGOTIATES = 506,
  STATUS_INSUFFICIDENT_STORAGE = 507,
  STATUS_LOOP_DETECTED = 508,
  STATUS_BANDWIDTH_LIMIT_EXCEEDED = 509,
  STATUS_NOT_EXTENDED = 510,
  STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511,
  STATUS_UNPARSEABLE_REPONSE_HEADERS = 600
}



function get(call, callback)
  Http.get(call.url, function(code, content)
    --print(call.url)
    if code == 200 then
      callback.onResponse(code, 
        call.worknet.converter.convert(content))
     else 
      callback.onFailure(code, content)
    end 
  end)
end

function _M:enqueue(callback)
  self.method(self, callback)
end

_M.__call = function(self, ...)
  local obj = table.clone(self)
  local args = {...}
  obj.worknet = args[1]
  obj.url = args[1].baseUrl..args[2].path
  obj.method = _ENV[args[2].method]
  return obj
end

return setmetatable(_M, _M)